package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.DishFlavordto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {

    }

    @Transactional
    @Override
    public void saveWithFlavor(DishFlavordto dishFlavordto) {
        //保存菜品基本信息;
        this.save(dishFlavordto);
        //获取保存的菜品ID;
        Long ids = dishFlavordto.getId();

        //获取菜品口味列表，遍历列表，为菜品口味对象属性dishId赋值;
        List<DishFlavor> flavors = dishFlavordto.getFlavors();

/*
        for (int i = 0; i < flavors.size(); i++) {
            flavors.get(i).setDishId(ids);
        }
*/

        //批量保存菜品口味列表;
        flavors = flavors.stream().map((item) -> {
            item.setDishId(ids);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishFlavordto GetByIdWithFlavor(Long id) {
        //从dish表中查出来基本信息
        Dish byId = this.getById(id);
        //把基本信息作为数据源复制给dto中一份
        DishFlavordto dishFlavordto = new DishFlavordto();
        BeanUtils.copyProperties(byId, dishFlavordto);
        //此时dto中的flavor菜品口味属性是空的，就要从dishFlavor中获取flavor病赋值给dto中的flavor属性然后返回
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishFlavordto.setFlavors(list);
        return dishFlavordto;
    }

    @Override
    @Transactional
    public void UpdateWithById(DishFlavordto dishFlavordto) {
        Long id = dishFlavordto.getId();
        String image = this.getById(id).getImage();

        if (!dishFlavordto.getImage().equals(image)){
            deletes(dishFlavordto.getId());
        }
        this.updateById(dishFlavordto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishFlavordto.getId());
        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishFlavordto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishFlavordto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /*    @Override
        public void deleteById(Long ids) {

            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getId, ids);
            Dish one = this.getOne(wrapper);

            boolean removedish = this.removeById(ids);

            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, ids);
            boolean removedishflaovr = dishFlavorService.remove(wrapper1);
            String s = "D:/img/" + one.getImage();
            log.info(s);
            File file = new File(s);
            file.delete();
        }*/
    @Override
    @Transactional
    public void deleteById(Long ids) {
        deletes(ids);
        this.removeById(ids);
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId, ids);
        dishFlavorService.remove(wrapper1);
    }

    //删除图片功能
    public void deletes(Long ids) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getId, ids);
        Dish one = this.getOne(wrapper);
        File file = new File("D:/img/" + one.getImage());
        file.delete();
    }

    @PostMapping("/status")
    public void status(int status,Long id){

    }
}
